layui.use(['grid', 'helper', 'jquery', 'table', 'view', 'request', 'layer'], function () {
    var grid = layui.grid
        , helper = layui.helper
        , view = layui.view
        , layer = layui.layer
        , request = layui.request
        , $ = layui.jquery;

    var tableId = "sys-menus-table";
    var remoteConfig = view.remoteConfig("/resource/admin/configs");

    var cols = [
            {width: 60, field: "LAY_NUM", title: "序号"},
            {field: "username", title: "管理员账号"},
            {field: "password", title: "管理员密码"},
            {field: "phone", title: "手机号"},
            {
                field: "status", title: "状态", templet: function (d) {
                    return view.toTableLabelText(d.status, remoteConfig["admin-state"])
                }
            },
            {
                field: "type", title: "类型", templet: function (d) {
                    return view.toTableLabelText(d.type, remoteConfig["admin-type"])
                }
            },
            {field: "update_time", title: "上次登录时间"},
            {fixed: 'right', width: 150, align: 'center', toolbar: '#tool'}
        ]

    grid.init(tableId, {
        "cols": [cols],
        "url": "/resource/admin/list",
        "create-event": { 'targetId': '#adminForm', 'title': '创建管理员', 'before': function () { $("#admin2-form")[0].reset(); }},
        "reset-password-event": {'title': '{{username}}-修改密码', 'url': '/admin/reset-password', 'sendData': function (value, row  ) { return {id: row.id, password: value}}},
        "delete-event": {"sendData": function (value, row) { return {conditions: [{key: "id", value : row.id}]}}},
    })

    /**
     * 查询数据
     */
    layui.form.on('submit(search)', function (data) {
        var field = data.field;
        grid.search(tableId, [
            {key: "username", value: field.username, type: "LIKE", 'filter_empty': true},
            {key: "phone", value: field.phone, type: "LIKE", 'filter_empty': true},
            {key: "status", value: field.status, 'filter_empty': true},
            {key: "type", value: field.type, 'filter_empty': true},
        ])
        return false;
    });

    layui.form.on('submit(reset)', function () {
        grid.search(tableId, []);
    })

    $(function () {
        view.renderSelect('admin-status-search', remoteConfig["admin-state"]);
        view.renderSelect('admin-type-search', remoteConfig["admin-type"]);
        // 重新渲染 select
        layui.form.render('select');
    })

    /**
     * 表单渲染
     */
    layui.form.on('submit(_SUBMIT_FORM)', function (data) {
        request.send("/admin/register", "POST", data.field, true, function (code, message, res) {
            checkResult(code, message, res, function () {
                layer.closeLast('page');
            })
        })
        return false;
    })

})
